package org.zttc.itat.service;




import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.zttc.itat.dao.INewsDao;
import org.zttc.itat.dao.ISortDao;
import org.zttc.itat.dao.ISortTypeDao;
import org.zttc.itat.dao.SortTypeDao;
import org.zttc.itat.exception.NewsException;
import org.zttc.itat.model.News;
import org.zttc.itat.model.Pager;
import org.zttc.itat.model.Sort;
import org.zttc.itat.model.SortType;
import org.zttc.itat.model.User;



@Service("newsService")
public class NewsService implements INewsService {
	
	@Resource(name="newsDao")
	private INewsDao newsDao;
	@Resource(name="sortDao")	
	private ISortDao sortDao;
	@Resource(name="sortTypeDao")
	private ISortTypeDao sortTypeDao;
	
	@Override
	public void add(News news, Integer sortId, Integer sorttypeId, User user) {
		Sort sort = sortDao.load(sortId);
		if(sort==null) throw new NewsException("必须添加一级栏目菜单");
		SortType st = sortTypeDao.load(sorttypeId);
		news.setSort(sort);
		news.setSortType(st);
		news.setUser(user);
		newsDao.add(news);
	}
	
	@Override
	public void add(News news, Integer sortId, User user) {
		Sort sort = sortDao.load(sortId);
		if(sort==null) throw new NewsException("必须添加一级栏目菜单");
		news.setSort(sort);
		news.setUser(user);
		newsDao.add(news);
	}


	
	@Override
	public void update(News news) {
		newsDao.update(news);
	}

	@Override
	public void delete(int id) {
		newsDao.delete(id);
	}

	@Override
	public News load(int id) {
		return newsDao.load(id);
	}

	@Override
	public Pager<News> findBySortIdAndUId(Integer sortId, Integer sortTypeId) {
		Pager<News> pages = null;
		if(sortId==null || sortId<=0){
			if(sortTypeId ==null || sortTypeId<=0){
			   pages = newsDao.find("from News");
			}else{
				pages = newsDao.find("select n from News where n.sortType.id=?",sortTypeId);
			}
		}else{
			if(sortTypeId ==null || sortTypeId<=0){
				pages = newsDao.find("select n from News n where n.sort.id=?",sortId);
			}else{
				pages = newsDao.find("select n from News n where n.sort.id=? and n.sortType.id=?",new Object[]{sortId,sortTypeId});
			}
		}
		return pages;
	}


	@Override
	public List<News> list(Integer id) {
		String hql = "select n from News n where n.sort.id=? or n.sortType.id=?";
		List<News> newslist = newsDao.list(hql,new Object[]{id,id});
		return newslist;
	}
	
	public Pager<News> findNews(String con){
		String hql = "select news from News news";
		if(con!=null){
			hql+=" where news.title like ? order by news.createDate desc";
			return newsDao.find(hql,new Object[]{"%"+con+"%"});
		}
		hql+=" order by news.createDate desc";
		return newsDao.find(hql);
	}

	@Override
	public Pager<News> findNews(String con, Integer sortId) {
		String hql = "select news from News news";
		if(con!=null){
		  hql+=" where news.title like ? and news.sort.id=? order by news.createDate desc";
		  return newsDao.find(hql,new Object[]{"%"+con+"%",sortId});
		}
		hql+="where news.sort.id=? order by news.createDate desc";
		 return newsDao.find(hql,new Object[]{sortId});
	}
}
